package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.SwitchView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -------需求
 */
public class RequiredActivity extends BaseActivity implements View.OnClickListener, SwitchView.OnStateChangedListener {

    private EditText et_required_person1;
    private EditText et_required_person2;
    private TextView tv_need_gift;
    private View rl_pick_message;
    private SwitchView sw_needPickup;

    private String inputPersonMessage1, inputPersonMessage2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_required);
        initData();
        getGift();

    }

    private ApplyMessagebean.NeedInfoEntity need_info;

    private void initData() {
        need_info = ApplyMessageActivity.bean.getNeed_info();
    }

    private List<GiftBean.InfoEntity> info1 = new ArrayList<>();

    private void getGift() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_GIFT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        GiftBean gift = gson.fromJson(result, GiftBean.class);
                        info1 = gift.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                initTitle();
                initView();
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("需求");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setText("保存");
        tv_right.setTextColor(Color.parseColor("#008c99"));
        tv_right.setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
    }

    private TextView tv_pick_message;

    private void initView() {
        tv_need_gift = (TextView) findViewById(R.id.tv_need_gift);
        et_required_person1 = (EditText) findViewById(R.id.et_required_person1);
        et_required_person2 = (EditText) findViewById(R.id.et_required_person2);
        sw_needPickup = (SwitchView) findViewById(R.id.sw_needPickup);

        rl_pick_message = findViewById(R.id.rl_pick_message);

        rl_pick_message.setVisibility(View.GONE);
        tv_pick_message = (TextView) findViewById(R.id.tv_pick_message);
//        findViewById(R.id.rl_needPickUp).setOnClickListener(this);
        if (!need_info.getNeed1().toString().equals("")) {
            et_required_person1.setText(need_info.getNeed1());
            et_required_person2.setText(need_info.getNeed2());
            tv_pick_message.setText(need_info.getCar_name());
            if ("1".equals(need_info.getIs_car())) {
                sw_needPickup.setOpened(true);
                showOrHidePickLayout(true);
            }
            if ("1".equals(need_info.getIs_gift())) {
                tv_need_gift.setText(need_info.getGift_name());
            }

        }

        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {
            et_required_person1.setEnabled(false);
            et_required_person2.setEnabled(false);

        } else {
            sw_needPickup.setOpened(false);
            sw_needPickup.setOnStateChangedListener(this);
            rl_pick_message.setOnClickListener(this);
            findViewById(R.id.rl_need_gift).setOnClickListener(this);
            findViewById(R.id.rl_required_others).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {

            case R.id.btn_main_left:

                finish();
                break;
            //保存
            case R.id.submit:

                inputPersonMessage1 = et_required_person1.getText().toString();
                inputPersonMessage2 = et_required_person2.getText().toString();
                if (inputPersonMessage1.toString().trim().equals("")) {
                    MyToast("请填写陪同人员需求");
                    return;
                }
                if (inputPersonMessage2.toString().trim().equals("")) {
                    MyToast("请填写公司接待人员需求");
                    return;
                }
                need_info.setNeed1(inputPersonMessage1);
                need_info.setNeed2(inputPersonMessage2);
                setResult(RESULT_OK);
                finish();
                break;
            //接车信息
            case R.id.rl_pick_message:
                intent = new Intent(this, GreetCartActivity.class);
                startActivityForResult(intent, 999);
                break;

            //礼品要求
            case R.id.rl_need_gift:
                //MyToast("礼品要求");
            {
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info1.size(); i++) {
                    str.add(info1.get(i).getGift_name());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_need_gift.setText(str.get(options1));
                        need_info.setIs_gift("1");
                        need_info.setGift_name("" + str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
            }

            break;

            //其它  TODO 跳转备注页面
            case R.id.rl_required_others:

                break;
            default:
                break;
        }
    }

    private void checkInputMessage() {

    }

    @Override
    public void toggleToOn(View view) {
        sw_needPickup.setOpened(true);
        showOrHidePickLayout(true);
    }

    @Override
    public void toggleToOff(View view) {
        sw_needPickup.setOpened(false);
        showOrHidePickLayout(false);
    }


    private void showOrHidePickLayout(boolean isNeedToPick) {

        if (isNeedToPick) {
            rl_pick_message.setVisibility(View.VISIBLE);
        } else {
            rl_pick_message.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 999:
                if (resultCode == RESULT_OK) {
                    tv_pick_message.setText(ApplyMessageActivity.bean.getNeed_info().getCar_name());
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
